package xmlprocess;

import entity.Customer;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author LamHuynh
 */
public class CustomerXMLProcess {

    private String fileName;

    public CustomerXMLProcess(String fileName) {
        this.fileName = fileName;
    }

    public Customer getCustomerInfo() {
        try {
            File fileXml = new File("Customers.xml");
            Customer customerReturn = new Customer();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder pars = factory.newDocumentBuilder();

            Document doc = pars.parse(fileXml);

            //


        } catch (SAXException ex) {
            Logger.getLogger(CustomerXMLProcess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CustomerXMLProcess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(CustomerXMLProcess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    //
    private void processNode(Node node, Customer customerReturn) {
        if (node.getNodeType() == node.ELEMENT_NODE) {
            String nodeName = node.getNodeName();
            if ("id".equalsIgnoreCase(nodeName)) {
                String id = node.getChildNodes().item(0).getNodeValue();
                customerReturn.setId(id);
            }else if ("name".equalsIgnoreCase(nodeName)) {
                String name = node.getChildNodes().item(0).getNodeValue();
                customerReturn.setName(name);
            }
        }

        NodeList childNode = node.getChildNodes();
        for (int i = 0; i < childNode.getLength(); i++) {
            processNode(childNode.item(i), customerReturn);
        }
    }
}
